package com.neusoft.elm.view.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Scanner;

import com.neusoft.elm.dao.FoodDao;
import com.neusoft.elm.dao.impl.FoodDaoImpl;
import com.neusoft.elm.po.Food;
import com.neusoft.elm.view.FoodView;

public class FoodViewImpl implements FoodView{
	
	private Scanner input = new Scanner(System.in);
	
	@Override
	public List<Food> showFoodList(Integer businessId) {	
		FoodDao dao = new FoodDaoImpl();
		List<Food> list = dao.listFoodByBusinessId(businessId);
		System.out.format("%-16s%-30s%-22s%-10s\n", "食品编号", "食品名称", "食品介绍", "食品价格");
		for(Food food : list) {
			System.out.format("%-10d%-20s%-22s%.2f\n", food.getFoodId(), food.getFoodName(), food.getFoodExplain(), food.getFoodPrice());
		}
	    return list; 
	}
		
	@Override
	public void saveFood(Integer businessId) {
		Food food = new Food();
		System.out.println("请输入食品名称：");
		food.setFoodName(input.next());
		System.out.println("请输入食品介绍：");
		food.setFoodExplain(input.next());
		System.out.println("请输入食品价格：");
		BigDecimal price = null;
		while (true) {
		    String priceStr = input.next();
		    try {
		        price = new BigDecimal(priceStr).setScale(2, RoundingMode.HALF_UP);
		        if (price.compareTo(BigDecimal.ZERO) <= 0) {
		            System.out.println("价格必须大于零，请重新输入:");
		        } else {
		            break;
		        }
		    } catch (NumberFormatException e) {
		        System.out.println("请输入有效的价格:");
		    }
		}
		food.setFoodPrice(price);
		food.setBusinessId(businessId);
		
		FoodDao dao = new FoodDaoImpl();
		int result = dao.saveFood(food);
		if(result>0) {
			System.out.println("\n新增食品成功!\n");
		}else {
			System.out.println("\n新增食品失败！\n");
		}	
	}
	
	@Override
	public void updateFood(Integer businessId) {
	    FoodDao dao = new FoodDaoImpl();
	    List<Food> list = showFoodList(businessId);

	    if (list.size() == 0) {
	        System.out.println("没有任何食品！");
	    } else {
	        System.out.println("请选择要更新的食品编号");
	        int foodId = input.nextInt();
	        Food food = dao.getFoodById(foodId);
	        System.out.println(food);

	        String inputStr = "";

	        while (true) {
	            System.out.println("\n请选择要更新的食品信息：");
	            System.out.println("1. 食品名称");
	            System.out.println("2. 食品介绍");
	            System.out.println("3. 食品价格");
	            System.out.println("4. 完成修改");

	            int option = input.nextInt();

	            switch (option) {
	                case 1:
	                    System.out.println("请输入新的食品名称：");
	                    food.setFoodName(input.next());
	                    break;
	                case 2:
	                    System.out.println("请输入新的食品介绍：");
	                    food.setFoodExplain(input.next());
	                    break;
	                case 3:
	                    System.out.println("请输入新的食品价格：");
	                    BigDecimal newPrice = null;
	                    while (true) {
	                        String priceStr = input.next();
	                        try {
	                            newPrice = new BigDecimal(priceStr).setScale(2, RoundingMode.HALF_UP);
	                            if (newPrice.compareTo(BigDecimal.ZERO) <= 0) {
	                                System.out.println("价格必须大于零，请重新输入:");
	                            } else {
	                                break;
	                            }
	                        } catch (NumberFormatException e) {
	                            System.out.println("请输入有效的价格:");
	                        }
	                    }
	                    food.setFoodPrice(newPrice);
	                    break;
	                case 4:
	                    int result = dao.updateFood(food);
	                    if (result > 0) {
	                        System.out.println("\n修改食品成功！\n");
	                    } else {
	                        System.out.println("\n修改食品失败！\n");
	                    }
	                    return;
	                default:
	                    System.out.println("无效的选项");
	                    break;
	            }
	        }
	    }
	}
	
	@Override
	public void removeFood(Integer businessId) {
	    FoodDao dao = new FoodDaoImpl();		
	    List<Food> list = showFoodList(businessId);

	    if (list.size() == 0) {
	        System.out.println("没有任何食品！");
	    } else {			
	        int foodId = 0;
	        boolean validFoodId = false;
	        while (!validFoodId) {
	            System.out.println("请选择要删除的食品编号");
	            while (!input.hasNextInt()) {
	                System.out.println("请输入有效的编号:");
	                input.next(); 
	            }
	            foodId = input.nextInt();
	            for (Food f : list) {
	                if (f.getFoodId() == foodId && f.getBusinessId() == businessId) {
	                    validFoodId = true;
	                    break;
	                }
	            }
	            if (!validFoodId) {
	                System.out.println("输入的编号不正确，请重新输入");
	            }
	        }

	        System.out.println("确认要删除吗？（y/n）");
	        if(input.next().equals("y")) {
	            int result = dao.removeFood(foodId);
	            if(result > 0) {
	                System.out.println("\n删除食品成功！\n");
	            } else {
	                System.out.println("\n删除食品失败！\n");
	            }
	        }	
	    }
	}
	
	@Override
	public void countFood(Integer businessId) {
	    FoodDao dao = new FoodDaoImpl();
	    int count = dao.countFoodByBusinessId(businessId);
	    System.out.println("食品数量：" + count);
	}
}
